package com.xinghanlaser.applet.base.common.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.xinghanlaser.applet.base.common.constant.OAuth2Constant.AUTHORIZATION_HEADER;
import static com.xinghanlaser.applet.base.common.constant.OAuth2Constant.SWAGGER_DEFECT_TOKEN;

/**
 * Swagger配置类
 * @author dym
 * @date 2019/9/21
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    private List<Parameter> buildParameters() {
        //添加head参数配置start
        final ParameterBuilder tokenPar = new ParameterBuilder();
        final List<Parameter> pars = new ArrayList<>();
        tokenPar.name(AUTHORIZATION_HEADER)
                .description("Token令牌")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .defaultValue(SWAGGER_DEFECT_TOKEN)
                .build();
        pars.add(tokenPar.build());
        //添加head参数配置end
        return pars;
    }

    @Bean
    public Docket docketApplet() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("com.xinghanlaser.applet.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .groupName("applet")
                .apiInfo(apiInfoApplet())
                .globalOperationParameters(buildParameters());
    }

    private ApiInfo apiInfoApplet() {
        return new ApiInfoBuilder().title("小程序相关API").description("")
                .version("2.0").build();
    }

    @Bean
    public Docket docketAuth() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select().apis(RequestHandlerSelectors.basePackage("com.xinghanlaser.applet.base.auth.controller"))
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build()
                .groupName("auth-admin")
                .apiInfo(apiInfoAuth())
                .globalOperationParameters(buildParameters());
    }

    private ApiInfo apiInfoAuth() {
        return new ApiInfoBuilder().title("权限相关API").description("")
                .version("2.0").build();
    }

}
